package cn.gxa.j229.bug.finance.service.loanuser.impl;

/*
 *
 *@author leijun
 *
 *@creating 2020/9/22 14:54
 *
 */

import cn.gxa.j229.bug.finance.mapper.InvestmentUserMapper;
import cn.gxa.j229.bug.finance.pojo.InvestmentUser;
import cn.gxa.j229.bug.finance.service.loanuser.InvestmentUserService;
import cn.gxa.j229.bug.finance.utils.MD5Util;
import cn.gxa.j229.bug.finance.utils.Salt;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @ClassName InvestmentUserImpl
 * @Description TODO
 * @Author 俊
 * @Date 2020/9/22 14:54
 * @Version 1.0
 */
@Service("investmentUserService")
public class InvestmentUserImpl implements InvestmentUserService {

    @Resource
    InvestmentUserMapper investmentUserMapper=null;


    @Override
    public List<Map<String, String>> findAllByPage(InvestmentUser investmentUser) {
        investmentUser.setPage((investmentUser.getPage()-1)*investmentUser.getLimit());
        return investmentUserMapper.findAllByPage(investmentUser);
    }

    @Override
    public List<Map<String, String>> findByName(InvestmentUser investmentUser) {
        investmentUser.setPage((investmentUser.getPage()-1)*investmentUser.getLimit());
        return investmentUserMapper.findByName(investmentUser);
    }

    @Override
    public int updateByPrimaryKeySelective(InvestmentUser investmentUser) {
        String password = investmentUser.getPassword();
        String trpassword = investmentUser.getTrpassword();
        if (password.trim().equals("") || password == null){
            return 0;
        }
        if (trpassword.trim().equals("") || trpassword == null){
            return 0;
        }
        String salt = Salt.createSalt();
        investmentUser.setSalt(salt);
        investmentUser.setPassword(MD5Util.encode(password+salt));
        investmentUser.setTrpassword(MD5Util.encode(trpassword+salt));
        investmentUser.setLogintime(new Date());
        investmentUser.setRegistration(new Date());
        return investmentUserMapper.updateByPrimaryKeySelective(investmentUser);
    }

    @Override
    public List<Map<String,String>> selectByPrimaryKey(Integer id) {
        return investmentUserMapper.selectByPrimaryKey(id);
    }

    @Override
    public int insertSelective(InvestmentUser investmentUser) {
        String password = investmentUser.getPassword();
        String trpassword = investmentUser.getTrpassword();
        if (!investmentUser.getPassword().equals(investmentUser.getRepassword())){
            return 0;
        }
        if (!investmentUser.getTrpassword().equals(investmentUser.getRetrpassword())){
          return 0;
        }
        String salt = Salt.createSalt();
        investmentUser.setSalt(salt);
        investmentUser.setPassword(MD5Util.encode(password+salt));
        investmentUser.setTrpassword(MD5Util.encode(trpassword+salt));
        return investmentUserMapper.insertSelective(investmentUser);
    }

    @Override
    public int countByPage() {
        return investmentUserMapper.countByPage();
    }

    @Override
    public int countByName(InvestmentUser investmentUser) {
        return investmentUserMapper.countByName(investmentUser);
    }
}
